import gdata.docs.data
import gdata.docs.client
import gdata.spreadsheet.service
import sys, os
from optparse import OptionParser

p = OptionParser()
p.add_option('-u', dest='user', help="Gmail account name")
p.add_option('-p', dest='password', help="Gmail password")
p.add_option('-n', dest='name', help="Account name")
p.add_option('-v', dest='ver', help="Version")
(opt, args) = p.parse_args()

if not (opt.user and opt.password and opt.name and opt.ver):
  p.print_help()
  sys.exit(0)

cdoc = gdata.docs.client.DocsClient()
cdoc.ssl = True
cdoc.ClientLogin(email=opt.user, password=opt.password, source='uoit-pages')

# Get the root folder
root = None
foldername = "pages - %s - %s" % (opt.name, opt.ver)
for f in cdoc.GetDocList(uri="/feeds/default/private/full/-/folder").entry:
  if f.title.text.startswith("Science Pages"):
    root = f
  if f.title.text == foldername:
    print "Target folder already exists"
    sys.exit(0)

cdoc.Create(gdata.docs.data.FOLDER_LABEL, foldername, root)

print 'Folder "%s" created.' % foldername
